package hmvictor.jpql;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.text.BadLocationException;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

@ActionID(
        category = "Edit",
        id = "hmvictor.jpql.PasteAction"
)
@ActionRegistration(
        displayName = "#CTL_PasteAction"
)
@ActionReference(path = "Menu/Edit", position = 1300)
@Messages("CTL_PasteAction=Paste from Java Code")
public final class PasteAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            JPQLEditorTopComponent editor=(JPQLEditorTopComponent) WindowManager.getDefault().findTopComponent("JQPLEditorTopComponent");
            editor.pasteQuery(Queries.getQueryFromJavaCode((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)));
        } catch (BadLocationException | IOException ex) {
            Exceptions.printStackTrace(ex);
        } catch (UnsupportedFlavorException ex) {
            
        }
    }
}
